<?php

class UserController extends BaseController{

    public function index(){
        $this->display();
    }

    public function login(){

        if($_POST){

            $post = I($_POST);
            $this->checkLogin($post);
            $res = db()->where( ['username'=>$post['username'] ] )->select("user");
            if(!$res) $this->error('没这号人','user/login');

            if(!password_verify($post['pwd'],$res[0]['pwd']))$this->error('密码错','user/login');
            $_SESSION['uid'] = $res[0]['id'];
            $_SESSION['username'] = $post['username'];
            $this->redirect('index/index');

        }


        $this->display();
    }
    public function register(){

        if($_POST){

            $post = I($_POST);
            $user = D('User')->getUser($post['username']);

            if($user) $this->error('有同名用户了','user/register');

            $res = D('User')->addUser($post);
            if($res){

                $this->success('注册成功','user/login');
            }
            $this->error('注册失败','user/register');

        }

        $this->display('login');
    }
    public function check($data){

        if(!$data['username'] || !$data['pwd']){
            $this->error('账号密码不能为空','user/login');
        }
        if($data['pwd']!= $data['pwd2']){
            $this->error('两次密码不一致','user/login');
        }
        if(!isset($data['code']) || !$data['code']){
            $this->error('验证码错','user/login');
        }
        $this->checkCode($data['code']);

    }

    public function checkLogin($data){
        //登录验证
        return true;
    }
    public function yzm(){
        $this->yanzhengma();
    }
    public function logout(){
        unset($_SESSION['username']);
        unset($_SESSION['uid']);
        $this->redirect('index/index');
    }


}

